using System;
using System.Collections.Generic;
using System.Drawing;
using MEA.AE_Data.Access;
using MEA.AE_Data.Config;

namespace MEA.AE_BusinessLogic
{
	public class FlashService : I_BusinessLogic, IFlashService
	{
		private IBaseService baseService;

		public event EventHandler<ServiceLayerBoolEventArg> ev_ProgressClose;

		public event EventHandler<ServiceLayerStringEventArg> ev_ProgressStep;

		public event BaseService.AddConsoleHandler ev_AddConsoleEvent;

		public event BaseService.LogFehlerHandler ev_LogFehler;

		public FlashService(IBaseService baseService)
		{
			this.baseService = baseService;
		}

		public void DataWriteByLID(List<byte> recValues)
		{
			if (recValues.Count != 9)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("FlashService.DataWriteByLID parameter has incorrect length", Color.Black, ConfigData.ECUString[(int)baseService.EcuId]);
				}
				throw new Exception("FlashService.DataWriteByLID parameter has incorrect length");
			}
			baseService.Diagnose_stop();
			baseService.SecuritySeedKey(5);
			baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Flash));
			new List<byte>();
			try
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Writing Data by LID (SID 0x" + string.Format("{0:X2}", ConfigKWP.SendList(ConfigKWP.e_kwpServices.Flash_DataWriteByLID)[0]) + ")", Color.Black, "ECU " + ConfigData.ECUString[(int)baseService.EcuId] + ": ");
				}
				foreach (byte recValue in recValues)
				{
					if (this.ev_AddConsoleEvent != null)
					{
						this.ev_AddConsoleEvent(string.Format("{0:X2}", recValue), Color.Black, "             ");
					}
				}
				baseService.AuxService(ConfigKWP.e_kwpServices.Flash_DataWriteByLID, recValues);
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("accepted", Color.Black, "            ");
				}
			}
			catch (ECUSendErrorException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unable to send request", Color.Black, "            ");
				}
				baseService.Diagnose_stop();
				baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
				baseService.TesterPresentStart(ConfigData.TesterPresentPeriod_ms);
			}
			catch (ECUResponseTimeoutException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Timeout error", Color.Black, "            ");
				}
				try
				{
					baseService.Diagnose_stop();
					baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
				}
				catch
				{
				}
			}
			catch (ECUResponseNegativeException ex3)
			{
				baseService.ErrorReportNegResponse(Convert.ToByte(ex3.Message));
			}
			catch (ECUResponseUnkownException ex4)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unknown Response: " + ex4.Message, Color.Black, "            ");
				}
			}
			catch (ECUDisconnectedExeption)
			{
			}
		}

		public void ECUResetFlash()
		{
			new List<byte>();
			if (this.ev_AddConsoleEvent != null)
			{
				this.ev_AddConsoleEvent("Reset ECU (SID 0x" + string.Format("{0:X2}", ConfigKWP.SendList(ConfigKWP.e_kwpServices.ECU_Reset)[0]) + ")", Color.Black, string.Concat("ECU ", baseService.EcuId, ": "));
			}
			baseService.AuxService(ConfigKWP.e_kwpServices.ECU_Reset, new List<byte>());
			if (this.ev_AddConsoleEvent != null)
			{
				this.ev_AddConsoleEvent("accepted", Color.Black, "            ");
			}
		}

		public List<byte> RequestResult(ConfigKWP.e_kwpServices sid)
		{
			List<byte> result = new List<byte>();
			if (sid != ConfigKWP.e_kwpServices.Flash_RequestResChkSum && sid != ConfigKWP.e_kwpServices.Flash_RequestResErase)
			{
				return result;
			}
			try
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Requesting result (SID 0x" + string.Format("{0:X2}", ConfigKWP.SendList(sid)[0]) + ")", Color.Black, string.Concat("ECU ", baseService.EcuId, ": "));
				}
				result = baseService.AuxService(sid, new List<byte>());
			}
			catch (ECUSendErrorException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unable to send request", Color.Black, "            ");
				}
				baseService.Diagnose_stop();
				baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
				return result;
			}
			catch (ECUResponseTimeoutException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Timeout error", Color.Black, "            ");
				}
				try
				{
					baseService.Diagnose_stop();
					baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
				}
				catch
				{
				}
				return result;
			}
			catch (ECUResponseNegativeException ex3)
			{
				baseService.ErrorReportNegResponse(Convert.ToByte(ex3.Message));
			}
			catch (ECUResponseUnkownException ex4)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unknown Response: " + ex4.Message, Color.Black, "            ");
				}
			}
			catch (ECUDisconnectedExeption)
			{
			}
			return result;
		}

		public List<byte> StartFlashErase(uint i_startAdr, uint i_EndAdr)
		{
			List<byte> list = new List<byte>();
			List<byte> collection = BaseService.TransformIntToByteList(i_startAdr, 3);
			List<byte> collection2 = BaseService.TransformIntToByteList(i_EndAdr, 3);
			baseService.TesterPresentHalt();
			List<byte> result = new List<byte>();
			try
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Starting fblock erase (SID 0x" + string.Format("{0:X2}", ConfigKWP.SendList(ConfigKWP.e_kwpServices.Flash_StartFlashErase)[0]) + ")", Color.Black, string.Concat("ECU ", baseService.EcuId, ": "));
				}
				string s = "Start: 0x" + string.Format("{0:X2}", i_startAdr) + " End: 0x" + string.Format("{0:X2}", i_EndAdr);
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent(s, Color.Black, "            ");
				}
				list.AddRange(collection);
				list.AddRange(collection2);
				result = baseService.AuxService(ConfigKWP.e_kwpServices.Flash_StartFlashErase, list);
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("accepted", Color.Black, "            ");
				}
			}
			catch (ECUSendErrorException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unable to send request", Color.Black, "            ");
				}
				baseService.Diagnose_stop();
				baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
				baseService.TesterPresentStart(ConfigData.TesterPresentPeriod_ms);
				return result;
			}
			catch (ECUResponseTimeoutException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Timeout error", Color.Black, "            ");
				}
				try
				{
					baseService.Diagnose_stop();
					baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
				}
				catch
				{
				}
				return result;
			}
			catch (ECUResponseNegativeException ex3)
			{
				baseService.ErrorReportNegResponse(Convert.ToByte(ex3.Message));
			}
			catch (ECUResponseUnkownException ex4)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unknown Response: " + ex4.Message, Color.Black, "            ");
				}
			}
			catch (ECUDisconnectedExeption)
			{
			}
			baseService.TesterPresentStart(ConfigData.TesterPresentPeriod_ms);
			return result;
		}

		public List<byte> StartChecksumCalculation(uint i_startAdr, uint i_EndAdr, uint i_checkSum)
		{
			List<byte> list = BaseService.TransformIntToByteList(i_checkSum, 4);
			List<byte> list2 = BaseService.TransformIntToByteList(i_startAdr, 3);
			List<byte> list3 = BaseService.TransformIntToByteList(i_EndAdr, 3);
			if (list2.Count != 3 || list3.Count != 3 || list.Count != 4)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("FlashService.StartChecksumCalculation: adress length not valid", Color.Black, ConfigData.ECUString[(int)baseService.EcuId]);
				}
				throw new Exception("FlashService.StartChecksumCalculation: adress length not valid");
			}
			List<byte> list4 = new List<byte>(10);
			baseService.TesterPresentHalt();
			List<byte> list5 = new List<byte>();
			if (this.ev_AddConsoleEvent != null)
			{
				this.ev_AddConsoleEvent("Starting checksum verification (SID 0x" + string.Format("{0:X2}", ConfigKWP.SendList(ConfigKWP.e_kwpServices.Flash_StartChecksumCalculation)[0]) + ")", Color.Black, string.Format("ECU {0}: ", ConfigData.ECUString[(int)baseService.EcuId]));
			}
			if (this.ev_AddConsoleEvent != null)
			{
				this.ev_AddConsoleEvent(string.Format("Start: 0x{0:X6}; End: 0x{1:X6}; CRC32: 0x{2:X8}", i_startAdr, i_EndAdr, i_checkSum), Color.Black, "            ");
			}
			list4.AddRange(list2);
			list4.AddRange(list3);
			list4.AddRange(list);
			list5 = baseService.AuxService(ConfigKWP.e_kwpServices.Flash_StartChecksumCalculation, list4);
			if (this.ev_AddConsoleEvent != null)
			{
				this.ev_AddConsoleEvent("accepted", Color.Black, "            ");
			}
			return list5;
		}

		public List<byte> TimingParameterAccess(ConfigFlash.e_timingParameter e_para)
		{
			baseService.TesterPresentHalt();
			List<byte> result = new List<byte>();
			try
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Accessing timing parameters (SID 0x" + string.Format("{0:X2}", ConfigKWP.SendList(ConfigKWP.e_kwpServices.ECU_TimingParameterAccess)[0]) + ")", Color.Black, string.Format("ECU {0}: ", ConfigData.ECUString[(int)baseService.EcuId]));
					this.ev_AddConsoleEvent(BaseService.TransformByteListToStringHex(ConfigFlash.TimingParameterGet(e_para), false), Color.Black, "              ");
				}
				result = baseService.AuxService(ConfigKWP.e_kwpServices.ECU_TimingParameterAccess, ConfigFlash.TimingParameterGet(e_para));
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("accepted", Color.Black, "            ");
				}
			}
			catch (ECUSendErrorException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unable to send request", Color.Black, "            ");
				}
				baseService.Diagnose_stop();
				baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
				baseService.TesterPresentStart(ConfigData.TesterPresentPeriod_ms);
				return result;
			}
			catch (ECUResponseTimeoutException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Timeout error", Color.Black, "            ");
				}
				try
				{
					baseService.Diagnose_stop();
					baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
				}
				catch
				{
				}
				return result;
			}
			catch (ECUResponseNegativeException ex3)
			{
				baseService.ErrorReportNegResponse(Convert.ToByte(ex3.Message));
			}
			catch (ECUResponseUnkownException ex4)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unknown Response: " + ex4.Message, Color.Black, "            ");
				}
			}
			catch (ECUDisconnectedExeption)
			{
			}
			baseService.TesterPresentStart(ConfigData.TesterPresentPeriod_ms);
			return result;
		}

		public void StopRoutine()
		{
			baseService.TesterPresentHalt();
			baseService.SecuritySeedKey(5);
			new List<byte>();
			try
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Stop routine (SID 0x" + string.Format("{0:X2}", ConfigKWP.SendList(ConfigKWP.e_kwpServices.Flash_StopRoutine)[0]) + ")", Color.Black, string.Format("ECU {0}: ", ConfigData.ECUString[(int)baseService.EcuId]));
				}
				baseService.AuxService(ConfigKWP.e_kwpServices.Flash_StopRoutine, new List<byte>());
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("accepted", Color.Black, "            ");
				}
			}
			catch (ECUSendErrorException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unable to send request", Color.Black, "            ");
				}
				baseService.Diagnose_stop();
				baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
				baseService.TesterPresentStart(ConfigData.TesterPresentPeriod_ms);
				return;
			}
			catch (ECUResponseTimeoutException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Timeout error", Color.Black, "            ");
				}
				try
				{
					baseService.Diagnose_stop();
					baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
					return;
				}
				catch
				{
					return;
				}
			}
			catch (ECUResponseNegativeException ex3)
			{
				baseService.ErrorReportNegResponse(Convert.ToByte(ex3.Message));
			}
			catch (ECUResponseUnkownException ex4)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unknown Response: " + ex4.Message, Color.Black, "            ");
				}
			}
			catch (ECUDisconnectedExeption)
			{
			}
			baseService.TesterPresentStart(ConfigData.TesterPresentPeriod_ms);
		}

		public void StopCHKRoutine()
		{
			new List<byte>();
			try
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Stop CHK routine (SID 0x" + string.Format("{0:X2}", ConfigKWP.SendList(ConfigKWP.e_kwpServices.Flash_StopRoutine)[0]) + ")", Color.Black, "ECU " + ConfigData.ECUString[(int)baseService.EcuId] + ": ");
				}
				baseService.AuxService(ConfigKWP.e_kwpServices.Flash_StopRoutine, new List<byte>());
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("accepted", Color.Black, "            ");
				}
			}
			catch (ECUSendErrorException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unable to send request", Color.Black, "            ");
				}
				baseService.Diagnose_stop();
				baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
				baseService.TesterPresentStart(ConfigData.TesterPresentPeriod_ms);
				return;
			}
			catch (ECUResponseTimeoutException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Timeout error", Color.Black, "            ");
				}
				try
				{
					baseService.Diagnose_stop();
					baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
					return;
				}
				catch
				{
					return;
				}
			}
			catch (ECUResponseNegativeException ex3)
			{
				baseService.ErrorReportNegResponse(Convert.ToByte(ex3.Message));
			}
			catch (ECUResponseUnkownException ex4)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unknown Response: " + ex4.Message, Color.Black, "            ");
				}
			}
			catch (ECUDisconnectedExeption)
			{
			}
			baseService.TesterPresentStart(ConfigData.TesterPresentPeriod_ms);
		}

		public List<byte> RequestDownload(uint i_startAdr, uint memSize)
		{
			List<byte> list = new List<byte>();
			List<byte> collection = BaseService.TransformIntToByteList(i_startAdr, 3);
			if (memSize == 0)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("FlashService.Flash_RequestDownload: memsize not valid", Color.Black, ConfigData.ECUString[(int)baseService.EcuId]);
				}
				throw new Exception("FlashService.Flash_RequestDownload memsize not valid");
			}
			List<byte> list2 = new List<byte>();
			if (this.ev_AddConsoleEvent != null)
			{
				this.ev_AddConsoleEvent("Request download to ECU (SID 0x" + string.Format("{0:X2}", ConfigKWP.SendList(ConfigKWP.e_kwpServices.Flash_RequestDownload)[0]) + ")", Color.Black, "ECU " + ConfigData.ECUString[(int)baseService.EcuId] + ": ");
			}
			list.AddRange(collection);
			list.Add(0);
			list.Add((byte)((memSize >> 16) & 0xFF));
			list.Add((byte)((memSize >> 8) & 0xFF));
			list.Add((byte)(memSize & 0xFF));
			list2 = baseService.AuxService(ConfigKWP.e_kwpServices.Flash_RequestDownload, list);
			if (this.ev_AddConsoleEvent != null)
			{
				this.ev_AddConsoleEvent("accepted", Color.Black, "            ");
			}
			return list2;
		}

		public void TransferData(byte blockSize, List<byte> data)
		{
			List<byte> list = new List<byte>();
			if (blockSize <= 0 || blockSize > 253)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("FlashService.TransferData: block size not valid", Color.Black, "              ");
				}
				throw new Exception("FlashService.TransferData block size not valid");
			}
			baseService.TesterPresentHalt();
			new List<byte>();
			list.AddRange(data);
			baseService.AuxService(ConfigKWP.e_kwpServices.Flash_TransferData, list);
		}

		public void TransferExit()
		{
			new List<byte>();
			if (this.ev_AddConsoleEvent != null)
			{
				this.ev_AddConsoleEvent(string.Format("Transfer exit (SID 0x{0:X2})", ConfigKWP.SendList(ConfigKWP.e_kwpServices.Flash_TransferExit)[0]), Color.Black, "ECU " + ConfigData.ECUString[(int)baseService.EcuId] + ": ");
			}
			baseService.AuxService(ConfigKWP.e_kwpServices.Flash_TransferExit, new List<byte>());
			if (this.ev_AddConsoleEvent != null)
			{
				this.ev_AddConsoleEvent("accepted", Color.Black, "            ");
			}
		}

		public List<byte> DataReadByLID()
		{
			baseService.Diagnose_stop();
			baseService.SecuritySeedKey(1);
			baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.MemoryAccess));
			List<byte> result = new List<byte>();
			try
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Read data by LID (SID 0x" + string.Format("{0:X2}", ConfigKWP.SendList(ConfigKWP.e_kwpServices.Flash_DataReadByLID)[0]) + ")", Color.Black, "ECU " + ConfigData.ECUString[(int)baseService.EcuId] + ": ");
				}
				result = baseService.AuxService(ConfigKWP.e_kwpServices.Flash_DataReadByLID, new List<byte>());
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("accepted", Color.Black, "            ");
				}
			}
			catch (ECUSendErrorException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unable to send request", Color.Black, "            ");
				}
				baseService.Diagnose_stop();
				baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
				return result;
			}
			catch (ECUResponseTimeoutException)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Timeout error", Color.Black, "            ");
				}
				try
				{
					baseService.Diagnose_stop();
					baseService.Diagnose_start(ConfigKWP.DiagMode_Get(ConfigKWP.e_DiagModes.Diagnostic));
				}
				catch
				{
				}
				return result;
			}
			catch (ECUResponseNegativeException ex3)
			{
				baseService.ErrorReportNegResponse(Convert.ToByte(ex3.Message));
			}
			catch (ECUResponseUnkownException ex4)
			{
				if (this.ev_AddConsoleEvent != null)
				{
					this.ev_AddConsoleEvent("Unknown Response: " + ex4.Message, Color.Black, "            ");
				}
			}
			catch (ECUDisconnectedExeption)
			{
			}
			return result;
		}
	}
}
